package beikai.com.assistant.service.impl;

import beikai.com.assistant.common.domain.params.UserVillageRelParams;
import beikai.com.assistant.common.exception.EleException;
import beikai.com.assistant.common.util.R;
import beikai.com.assistant.dao.mapper.UserVillageRelMapper;
import beikai.com.assistant.dao.model.UserVillageRelModel;
import beikai.com.assistant.service.IUserVillageRelService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 小区信息 服务实现类
 * </p>
 *
 * @author beikai
 * @since 2021-12-19
 */
@Service
@Slf4j
public class UserVillageRelServiceImpl extends ServiceImpl<UserVillageRelMapper, UserVillageRelModel> implements IUserVillageRelService {

    @Override
    public JSONObject addRel(UserVillageRelParams userVillageRelParams) {
        int count = count(Wrappers.lambdaQuery(new UserVillageRelModel())
                .eq(UserVillageRelModel::getIsDelete, false)
                .eq(UserVillageRelModel::getUserId, userVillageRelParams.getUserId())
                .eq(UserVillageRelModel::getVillageId, userVillageRelParams.getVillageId())
        );
        if (count > 0) {
            return R.success();
        }
        UserVillageRelModel userVillageRelModel = new UserVillageRelModel();
        userVillageRelModel.setVillageId(userVillageRelParams.getVillageId());
        userVillageRelModel.setUserId(userVillageRelParams.getUserId());
        if (save(userVillageRelModel)) {
            return R.success();
        }
        log.error("添加的参数是[{}]",JSONObject.toJSONString(userVillageRelParams));
        throw new EleException("添加关系失败");
    }
}
